package com.example.fiction.utils;

import android.text.TextUtils;
import android.util.Log;

import java.io.IOException;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 数据传递工具类
 * author: 姚鸿杰
 * time: 2019/4/13 20:52
 **/
public class TransferDataUtils {

    ////////////////////////////////
    /////////////RSA解密////////////
    ////////////////////////////////

    private static final String ALGORITHM_RSA = "RSA/ECB/PKCS1Padding";

    /**
     * 公匙验签
     *
     * @param text：签名文本
     * @param sign：签名串
     * @return 签名结果
     */
    public static boolean verifyRSA(String text, String sign) {
        try {
            Signature signature = Signature.getInstance("SHA1withRSA"); //签名算法
            //获取公匙
            byte[] publicKey = readKey("rsa_public_key.pem");
            PublicKey key = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKey));
            signature.initVerify(key);
            signature.update(text.getBytes());
            return signature.verify(Base64Utils.decode(sign));
        } catch (Exception e) {
            Log.e("验签失败", e.getMessage());
        }
        return false;
    }


    /**
     * RSA公匙加密
     *
     * @param rawData 加密数据
     * @return 加密串
     * @throws IOException
     */
    public static String encrypterPublicRSA(String rawData) throws IOException{

        if (TextUtils.isEmpty(rawData)) return null;

        byte[] data = rawData.getBytes();   //将数据转换为二进制
        //获取公匙
        byte[] publicKey = readKey("rsa_public_key.pem");

        byte[] result = EncryptUtils.encryptRSA(data, publicKey, true, ALGORITHM_RSA);
        return Base64Utils.encode(result);
    }

    /**
     * RSA公匙解密
     *
     * @param decryption 解密串
     * @return 解密数据
     * @throws IOException
     */
    public static String decrypterPublicRSA(String decryption) throws IOException {
        byte[] data = Base64Utils.decode(decryption);   //将数据转换为二进制
        //获取公匙
        byte[] publicKey = readKey("rsa_public_key.pem");

        byte[] result = EncryptUtils.decryptRSA(data, publicKey, true, ALGORITHM_RSA);
        return new String(result, "UTF-8");
    }

    /**
     * 获取公匙
     *
     * @param publicKey 公匙文件名
     * @return 二进制公匙
     * @throws IOException
     */
    private static byte[] readKey(String publicKey) throws IOException {
        return Base64Utils.decode(ReadAssets.readPathText(publicKey));
    }

    ////////////////////////////////
    /////////////AES解密////////////
    ////////////////////////////////

    private static final String IV = "yaohongjie970101";
    private static final String ALGORITHM_AES = "AES/CBC/PKCS7Padding";

    /**
     * @return 返回当前时间
     */
    public static String getAESPassword() {
        Date dNow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddHHmmss");
        return ft.format(dNow) + "hj";
    }

    /**
     * AES加密
     *
     * @param rawData 加密数据
     * @return 加密串
     */
    public static String encrypterAES(String rawData, String key) {

        if (TextUtils.isEmpty(rawData)) {
            return null;
        }
        byte[] data = rawData.getBytes();
        //设置密匙
        byte[] encrypter = EncryptUtils.encryptAES2Base64(data, key.getBytes(), ALGORITHM_AES, IV.getBytes());
        return new String(encrypter);
    }

    public static String decrypterAES(String decryption, String key) {
        byte[] decrypter = EncryptUtils.decryptBase64AES(decryption.getBytes(), key.getBytes(), ALGORITHM_AES, IV.getBytes());
        return new String(decrypter);
    }

}
